package GMDPRE

func KDF(outLen int, bytesIn []uint8, inLen int) []uint8 {
	var res []uint8
	var ctx Sm3Ctx
	var i int
	var ct uint32 = 0

	for i = 0; i < (outLen >> 5); i++ {
		Sm3Init(&ctx)
		Sm3Update(&ctx, bytesIn, uint64(inLen))
		Sm3Update(&ctx, down4bytes(ct), 4)
		res = append(res, Sm3Finish(&ctx)...)
		ct++
	}

	if outLen&0x1f != 0 {
		Sm3Init(&ctx)
		Sm3Update(&ctx, bytesIn, uint64(inLen))
		Sm3Update(&ctx, down4bytes(ct), 4)
		res = append(res, Sm3Finish(&ctx)[:outLen&0x1f]...)
	}

	return res
}
